! function() {
    'use strict';

    // 存储所有提示框
    var modalMap = {};

    var exports = {
        modalMap: modalMap,
        // 操作提示
        tips: function() {
            var content = arguments[0];
            var time = typeof arguments[1] == 'number' ? arguments[1] : 1600;
            var complete = typeof arguments[1] == 'function' ? arguments[1] : (arguments[2] || function() {});
            var modal = new mainTips();
            modal.init(content);
            modal.show();
            setTimeout(function() {
                modal.hide();
                setTimeout(function() {
                    modal.remove();
                    modal = null;
                    complete();
                }, 200);
            }, time);
        },
        // 消息提示
        msg: function() {
            var content = arguments[0];
            var time = typeof arguments[1] == 'number' ? arguments[1] : 1600;
            var complete = typeof arguments[1] == 'function' ? arguments[1] : (arguments[2] || function() {});
            var modal = new mainMsg();
            modal.init(content);
            modal.show();
            setTimeout(function() {
                modal.hide();
                setTimeout(function() {
                    modal.remove();
                    modal = null;
                    complete();
                }, 200);
            }, time);
        },
        // 加载提示
        load: function() {
            var modal = new mainLoad();
            modalMap[modal.id] = modal;
            modal.init();
            modal.show();
            return modal.id;
        },
        // 内容提示
        popup: function(opts) {
            var modal = new mainPopup(opts || {});
            modalMap[modal.id] = modal;
            modal.init();
            modal.show();
            return modal.id;
        },
        // 询问框
        confirm: function(opts) {
            var modal = new mainConfirm(opts || {});
            modalMap[modal.id] = modal;
            modal.init();
            modal.show();
        },
        // 关闭单个
        close: closeModal,
        // 关闭全部
        closeAll: function() {
            for(var i in modalMap) {
                closeModal(i);
            }
        }
    };

    // 关闭某个modal
    function closeModal(id, callback) {
        modalMap[id].hide();
        setTimeout(function() {
            modalMap[id].remove();
            delete modalMap[id];
            typeof callback == 'function' ? callback() : void(0);
        }, 200);
    }

    // 创建唯一标识
    function GenNonDuplicateID(randomLength) {
        return Number(Math.random().toString().substr(3, randomLength) + Date.now()).toString(36);
    }

    // mainTips -- main
    function mainTips() {
        var id = GenNonDuplicateID();
        this.id = id;
    }

    // mainTips -- init
    mainTips.prototype.init = function(content) {
        var self = this;
        var modalDom = document.createElement('div');
        modalDom.id = self.id;
        modalDom.className = 'modalTips';
        var ele = '';
        ele += '<font class="modalTipsContent">';
        ele += content;
        ele += '</font>';
        modalDom.innerHTML = ele;
        document.body.appendChild(modalDom);
    };

    // mainTips -- show
    mainTips.prototype.show = function() {
        var self = this;
        var modalDom = document.getElementById(self.id);
        modalDom.className = 'modalTips toggleUp';
    };

    // mainTips -- hide
    mainTips.prototype.hide = function() {
        var self = this;
        var modalDom = document.getElementById(self.id);
        modalDom.className = 'modalTips toggleDown';
    };

    // mainTips -- remove
    mainTips.prototype.remove = function() {
        var self = this;
        var modalDom = document.getElementById(self.id);
        modalDom.parentNode.removeChild(modalDom);
    };

    // mainMsg -- main
    function mainMsg() {
        var id = GenNonDuplicateID();
        this.id = id;
    }

    // mainMsg -- init
    mainMsg.prototype.init = function(content) {
        var self = this;
        var modalDom = document.createElement('div');
        modalDom.id = self.id;
        modalDom.className = 'modalMsg';
        var ele = '';
        ele += '<div class="modalMsgContent">';
        ele += content;
        /*ele += '<marquee behavior="scroll" scrolldelay="160">';
        ele += content;
        ele += '</marquee>';*/
        ele += '</div>';
        modalDom.innerHTML = ele;
        document.body.appendChild(modalDom);
    };

    // mainMsg -- show
    mainMsg.prototype.show = function() {
        var self = this;
        var modalDom = document.getElementById(self.id);
        modalDom.className = 'modalMsg toggleDown';
    };

    // mainMsg -- hide
    mainMsg.prototype.hide = function() {
        var self = this;
        var modalDom = document.getElementById(self.id);
        modalDom.className = 'modalMsg toggleUp';
    };

    // mainMsg -- remove
    mainMsg.prototype.remove = function() {
        var self = this;
        var modalDom = document.getElementById(self.id);
        modalDom.parentNode.removeChild(modalDom);
    };

    // mainLoad -- main
    function mainLoad() {
        var id = GenNonDuplicateID();
        this.id = id;
    }

    // mainLoad -- init
    mainLoad.prototype.init = function() {
        var self = this;
        var modalDom = document.createElement('div');
        modalDom.id = self.id;
        modalDom.className = 'modalLoad';
        var ele = '';
        ele += '<div class="modalLoadBubble">';
        ele += '<div class="modalLoadBubble1"></div>';
        ele += '<div class="modalLoadBubble2"></div>';
        ele += '</div>';
        modalDom.innerHTML = ele;
        document.body.appendChild(modalDom);
    };

    // mainLoad -- show
    mainLoad.prototype.show = function() {
        var self = this;
        var modalDom = document.getElementById(self.id);
        modalDom.className = 'modalLoad toggleDown';
    };

    // mainLoad -- hide
    mainLoad.prototype.hide = function() {
        var self = this;
        var modalDom = document.getElementById(self.id);
        modalDom.className = 'modalLoad toggleUp';
    };

    // mainLoad -- remove
    mainLoad.prototype.remove = function() {
        var self = this;
        var modalDom = document.getElementById(self.id);
        modalDom.parentNode.removeChild(modalDom);
    };

    // mainPopup -- main
    function mainPopup(opts) {
        var id = GenNonDuplicateID();
        this.id = id;
        var opt = tool.optionExtend({
            shade: '',
            shadeClose: false,
            container: document.body,
            toggle: 'down',
            className: '',
            content: ''
        }, opts);
        this.opt = opt;
    }

    // mainPopup -- init
    mainPopup.prototype.init = function() {
        var self = this;
        var modalDom = document.createElement('div');
        modalDom.id = self.id;
        modalDom.className = 'modalPopup ' + self.opt.shade;
        if(self.opt.shadeClose) {
            modalDom.onclick = function(e) {
                var e = e || window.event;
                if(e.stopPropagation) {
                    e.stopPropagation();
                } else {
                    e.cancelBubble = true;
                }
                var className = e.target.className;
                var reg = /modalPopup\b/gi;
                if(reg.test(className)) {
                    this.onclick = null;
                    closeModal(self.id);
                }
            };
        }
        var ele = '';
        ele += '<div class="modalPopupContainer ' + self.opt.className + '">';
        ele += self.opt.content;
        ele += '</div>';
        modalDom.innerHTML = ele;
        self.opt.container.appendChild(modalDom);
    };

    // mainPopup -- show
    mainPopup.prototype.show = function() {
        var self = this;
        var modalDom = document.getElementById(self.id);
        modalDom.className = 'modalPopup toggleShow ' + self.opt.toggle + ' ' + self.opt.shade;
    };

    // mainPopup -- hide
    mainPopup.prototype.hide = function() {
        var self = this;
        var modalDom = document.getElementById(self.id);
        modalDom.className = 'modalPopup toggleHide ' + self.opt.toggle + ' ' + self.opt.shade;
    };

    // mainPopup -- remove
    mainPopup.prototype.remove = function() {
        var self = this;
        var modalDom = document.getElementById(self.id);
        modalDom.parentNode.removeChild(modalDom);
    };

    // mainConfirm -- main
    function mainConfirm(opts) {
        var id = GenNonDuplicateID();
        this.id = id;
        var opt = tool.optionExtend({
            shade: '',
            className: '',
            content: '',
            btn: ['关闭'],
            btnFn: [closeModal, closeModal]
        }, opts);
        this.opt = opt;
    }

    // mainConfirm -- init
    mainConfirm.prototype.init = function() {
        var self = this;
        var modalDom = document.createElement('div');
        modalDom.id = self.id;
        modalDom.className = 'mainConfirm v-valign ' + self.opt.shade;
        var ele = '';
        ele += '<div class="mainConfirmContainer ' + self.opt.className + '">';
        ele += '<div class="mainConfirmContent">';
        ele += self.opt.content;
        ele += '</div>';
        ele += '<div class="mainConfirmFooter">';
        // 获取按钮长度
        var len = self.opt.btn.length - 1;
        for(var i = len; i >= 0; i--) {
            ele += '<div class="cell">';
            ele += '<a href="javascript:;" class="mainConfirmBtn">' + self.opt.btn[i] + '</a>';
            ele += '</div>';
        }
        ele += '</div>';
        ele += '</div>';
        modalDom.innerHTML = ele;
        // 获取按钮
        var btnList = modalDom.getElementsByTagName('a');
        // 绑定点击事件
        for(var i = 0; i < btnList.length; i++) {
            if(btnList[i].className == 'mainConfirmBtn') {
                btnList[i].setAttribute('data-idx', i);
                btnList[i].onclick = function(e) {
                    this.onclick = null;
                    var idx = this.getAttribute('data-idx');
                    self.opt.btnFn[len - idx](self.id);
                };
            }
        }
        document.body.appendChild(modalDom);
    };

    // mainConfirm -- show
    mainConfirm.prototype.show = function() {
        var self = this;
        var modalDom = document.getElementById(self.id);
        modalDom.className = 'mainConfirm v-valign ' + self.opt.shade + ' zoomIn';
    };

    // mainConfirm -- hide
    mainConfirm.prototype.hide = function() {
        var self = this;
        var modalDom = document.getElementById(self.id);
        modalDom.className = 'mainConfirm v-valign ' + self.opt.shade + ' zoomOut';
    };

    // mainConfirm -- remove
    mainConfirm.prototype.remove = function() {
        var self = this;
        var modalDom = document.getElementById(self.id);
        modalDom.parentNode.removeChild(modalDom);
    };

    window.modal = exports;

}();